/**
 * @file    friendrequestswindow.cpp
 * @author  Siddharth Sathyam <ssathy2@gmail.com>
 * @author  Alan Ruvalcaba <aruval3@gmail.com>
 * @author  Pat LaMontagna <plamontagna@gmail.com>
 * @version 1.0
 * @section LICENSE
 *
 * This program is property of Siddharth Sathyam, Alan Ruvalcaba, Pat LaMontagna.
 *
 * @section DESCRIPTION
 *
 * The friendrequestswindow holds all of the friend requests that belong to the user.
 */

#include "friendrequestswindow.h"
#include "ui_friendrequestswindow.h"

friendRequestsWindow::friendRequestsWindow(QWidget *parent, QString personRecieving) :
    QDialog(parent),
    ui(new Ui::friendRequestsWindow)
{
    personReceivingRequests = personRecieving;
    ui->setupUi(this);
}

friendRequestsWindow::~friendRequestsWindow()
{
    delete ui;
}

void friendRequestsWindow::handleFriendshipConfirmation(QString personSendingRequest)
{
    // person isn't in the request list so we add them to the list
    if(!listOfCurrentRequests.contains(personSendingRequest) && (personSendingRequest != personReceivingRequests))
    {
	listOfCurrentRequests.push_front(personSendingRequest);
	ui->requestListWidget->addItem(personSendingRequest);
	this->show();
    }

    // person is already in the list...
    else
    {
	//@TODO Person already in the friend request list, figure out what to do here
    }
}

void friendRequestsWindow::on_acceptButton_clicked()
{
    if(ui->requestListWidget->selectedItems().count() == 1)
    {
	// Hope that this conversion works....
	QListWidgetItem* personToAccept = ui->requestListWidget->selectedItems().takeFirst();
	QString personToAcceptName = personToAccept->text();
	listOfCurrentRequests.removeAll(personToAcceptName);
	ui->requestListWidget->removeItemWidget(personToAccept);
	emit confirmFriendRequest(personToAcceptName, true);
	delete personToAccept;
    }
    else
    {
	QMessageBox errorWindow;
	errorWindow.setText("You haven't selected any requests");
	errorWindow.setModal(Qt::ApplicationModal);
	errorWindow.exec();

    }
}

void friendRequestsWindow::on_denyButton_clicked()
{
    if(ui->requestListWidget->selectedItems().count() == 1)
    {
	// Hope that this conversion works....
	QListWidgetItem* personToAccept = ui->requestListWidget->selectedItems().takeFirst();
	QString personToAcceptName = personToAccept->text();
	ui->requestListWidget->removeItemWidget(personToAccept);
	listOfCurrentRequests.removeAll(personToAcceptName);
	emit confirmFriendRequest(personToAcceptName, false);
	delete personToAccept;
    }
    else
    {
	QMessageBox errorWindow;
	errorWindow.setText("You haven't selected any requests");
	errorWindow.setModal(Qt::ApplicationModal);
	errorWindow.exec();
    }
}
